package cn.hfzy.controller;

import com.sun.org.apache.bcel.internal.classfile.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: Administrator
 * @Date: 2018/12/19 15:30
 * @Description:
 */
@Controller
@RequestMapping("call")
public class CallController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${callService.url}")
    String callService;

    @Value("${cloudCallWs.url}")
    String cloudCallWs;

    @Value("${freeswitch.ws}")
    String freeswitchWs;

    @Value("${freeswitch.userurl}")
    String userurl;

    @Value(("${web.checkoutUrl}"))
    String checkoutUrl;

    /**
     *  转跳到freeswitchCallDemo
     *  https://www.ihk.cn:9090/fsdemo/call/fsCall?userName=13000001234&phoneNum=15279615654&cityId=1&createUserId=11&customerName=李四
     * @param userName 用于freeswitch注册的名称
     * @param phoneNum 客户的手机号
     * @param cityId 城市id
     * @param createUserId 呼叫经纪id
     * @param customerName 发起呼叫人的名字
     * @return
     */
    @RequestMapping("fsCall")
    public ModelAndView fsCall( String userName,String phoneNum,
                                String cityId,String createUserId,String customerName) {
        ModelAndView mv = new ModelAndView();

        mv.addObject("callService", callService);

        mv.addObject("cloudCallWs", cloudCallWs);

        mv.addObject("freeswitchWs", freeswitchWs);

        mv.addObject("userurl", userurl);

        mv.setViewName("page/new/testFreeswitchCall");

        if (userName != null && !"".equals(userName) && phoneNum != null && !"".equals(phoneNum)
                && cityId != null && !"".equals(cityId) && createUserId != null && !"".equals(createUserId)
                && customerName != null && !"".equals(customerName)) {
            String targetUrl = checkoutUrl + "?userName=" + userName + "&clientType=IHK";
            //调用接口校验用户名
            Map map = restTemplate.getForObject(targetUrl, HashMap.class);

            if (map.get("code") != null && !"".equals(map.get("code"))) {
                if ("1".equals(map.get("code") + "")) {
                    mv.addObject("msg", map.get("msg"));
                    mv.addObject("username", userName);
                    mv.addObject("phoneNum", phoneNum);
                    mv.addObject("cityId", cityId);
                    mv.addObject("createUserId", createUserId);
                    mv.addObject("customerName", customerName);
                } else {
                    mv.addObject("msg", map.get("msg"));
                }
            }
        }
        return mv;
    }

    @RequestMapping("test")
    public ModelAndView testCall() {
        ModelAndView mv = new ModelAndView();

        mv.addObject("callService", callService);

        mv.addObject("cloudCallWs", cloudCallWs);

        mv.addObject("freeswitchWs", freeswitchWs);

        mv.addObject("userurl", userurl);

        mv.setViewName("page/test/testFreeswitchCall");

        return mv;
    }

    @RequestMapping("newFs")
    public String newFs(Model model){

        model.addAttribute("callService",callService);

        model.addAttribute("cloudCallWs",cloudCallWs);

        model.addAttribute("freeswitchWs",freeswitchWs);

        model.addAttribute("userurl",userurl);

        return "page/new/testFreeswitchCall";
    }
}
